﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MediaClient.Common;
using MediaDataModel;
using MediaDataModel.Common;
using MediaDataModel.Common.Other;
using System.IO;

namespace MediaClient.ImportMedia
{
    
    public partial class ImportConfigForm : BaseSmallForm
    {
        private Dictionary<string,short> dicCaptureType = new Dictionary<string,short>{{"标清（576p）",0},{"小高清（720p）",1},{"高清（1080i）",2},{"高清（1080psf）",3},{"高清（1080p）",4}};

        private Dictionary<string, short> dicVideoExtesion = new Dictionary<string, short> { { "MPG（IBP422）", 16 }, { "AVI（全I帧）", 0 }, { "MXF_XDCAM_HD（XDCAM）", 256 }, { "MXF_MPEG2（MXF MPEG2）", 512 }, { "MXF_P2（P2）", 32 } };

        private Dictionary<string, List<KeyValuePair<string,short>>> relationShip = new Dictionary<string, List<KeyValuePair<string,short>>> ();

        public ImportConfigForm()
        {
            InitializeComponent();
        }

        public ImportConfigForm(string importFlag):base()
        {
            switch (importFlag)
            {
                
                case "File":
                    lblLogo.Image = global::MediaClient.Properties.Resources.弹出窗口文件录制;
                    break;
                case "SDI":
                default:
                    lblLogo.Image = global::MediaClient.Properties.Resources.弹出窗口SDI录制;
                    break;
            }
        }

        private void ImportConfig_Load(object sender, EventArgs e)
        {
            try
            {
                this.CaptureConfig = new ImporConfig();
                XmlNodeList nodeList = Utility.GetNodes(BaseForm.systemConfigFile, "video/bitRate/import/rate");
                if (nodeList != null)
                {
                    foreach (XmlNode childNode in nodeList)
                    {
                        KeyValue oVideo = new KeyValue { Value = childNode.Attributes["value"].Value.ToString(), Key = childNode.Attributes["name"].Value.ToString() };
                        cmbVideoRate.Items.Add(oVideo);
                    }
                    cmbVideoRate.SelectedIndex = 0;
                }

                nodeList = Utility.GetNodes(BaseForm.systemConfigFile, "disc/capacity");
                if (nodeList != null)
                {
                    foreach (XmlNode childNode in nodeList)
                    {
                        KeyValue oDisc = new KeyValue { Value = childNode.Attributes["value"].Value.ToString(), Key = childNode.Attributes["name"].Value.ToString() };
                        cmbDiscType.Items.Add(oDisc);
                    }
                    cmbDiscType.SelectedIndex = 0;
                }

                InitializeForm();
            }
            catch (Exception myE)
            {
            }
            
        }

        private void lblSubmit_MouseMove(object sender, MouseEventArgs e)
        {
            lblSubmit.Image = global::MediaClient.Properties.Resources.确定_1;
        }

        private void lblSubmit_MouseLeave(object sender, EventArgs e)
        {
            lblSubmit.Image = global::MediaClient.Properties.Resources.确定;
        }

        private void lblSubmit_Click(object sender, EventArgs e)
        {
            this.txtDiscName.Focus();
            if (txtDiscName.Text.Trim() == "")
            {
                MessageBox.Show("请输入光盘名称。", BaseForm.MessageBoxCaption);
                txtDiscName.Focus();
                return;
            }
            FileStream captureAppConfigStream = null;
            StreamReader configReader = null;
            StreamWriter configWriter = null;
            try
            {
                captureAppConfigStream = new FileStream(BaseForm.capatureAppConfig, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                configReader = new StreamReader(captureAppConfigStream);
                string configContent = configReader.ReadToEnd();
                captureAppConfigStream.Seek(0, SeekOrigin.Begin);

                KeyValue selectedCaptrueType = cmbCaptureType.SelectedItem as KeyValue;
                KeyValue selectFormat = cmbVideoFormat.SelectedItem as KeyValue;
                if (selectedCaptrueType != null && selectFormat != null)
                {
                    //while (configReader.EndOfStream == false)
                    //{
                    //    string temp = configReader.ReadLine();
                    //    if (temp.ToLower().StartsWith("avtype") == true)
                    //        configContent = configContent.Replace(temp, "AVType = " + BaseForm.videoAudioSplit);
                    //    else if (temp.ToLower().StartsWith("dimension") == true)
                    //        configContent = configContent.Replace(temp, "Dimension = " + selectedCaptrueType.Value);
                    //    else if (temp.ToLower().StartsWith("capturefiletype") == true)
                    //        configContent = configContent.Replace(temp, "CaptureFileType = " + selectFormat.Value);
                    //    else if (temp.ToLower().StartsWith("surfaceformat") == true)
                    //    {
                    //        foreach (KeyValuePair<string, short> format in relationShip[selectedCaptrueType.Key])
                    //        {
                    //            if (format.Key == selectFormat.Key)
                    //            {
                    //                configContent = configContent.Replace(temp, "SurfaceFormat = " + format.Value);
                    //                break;
                    //            }
                    //        }
                    //    }
                    //    else if (temp.ToLower().StartsWith("videobitrate") == true)
                    //        configContent = configContent.Replace(temp, "VideoBitRate=" + (cmbVideoRate.SelectedItem as KeyValue).Value);
                    //}
                    //configReader.Close();
                    //configReader.Dispose();
                    //configWriter = new StreamWriter(BaseForm.capatureAppConfig, false);
                    //configWriter.WriteLine(configContent);
                    //configWriter.Flush();
                     if (rabDouble.Checked == true)
                         this.CaptureConfig.BurnType = 2;
                     else
                         this.CaptureConfig.BurnType = 1;
                     if (selectFormat.Value == "0")
                         this.CaptureConfig.VideoExtension = ".avi";
                     else if (selectFormat.Value == "16")
                         this.CaptureConfig.VideoExtension = ".mpg";
                     else
                         this.CaptureConfig.VideoExtension = ".mxf";
                     this.CaptureConfig.VideoRate = int.Parse((cmbVideoRate.SelectedItem as KeyValue).Value);
                     this.CaptureConfig.DiscCapacity = ulong.Parse((cmbDiscType.SelectedItem as KeyValue).Value);
                     if (chkRealTimeBurn.Checked == true)
                         this.CaptureConfig.RealTimeBurn = true;
                     else
                         this.CaptureConfig.RealTimeBurn = false;
                     this.CaptureConfig.DiscName = txtDiscName.Text.Trim();
                     this.Close();
                 }
            }
            catch (Exception myE)
            {
                throw myE;
            }
            finally
            {
                if (configReader != null)
                {
                    configReader.Close();
                    configReader.Dispose();
                }
                if (configWriter != null)
                {
                    configWriter.Close();
                    configWriter.Dispose();
                }
            }
        }

        private void cmbCaptureType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbVideoFormat.Items.Clear();
            KeyValue oSelected = cmbCaptureType.SelectedItem as KeyValue;
            if(oSelected == null)return;

            foreach (string key in relationShip.Keys)
            {
                if (key == oSelected.Key)
                {
                    foreach (KeyValuePair<string, short> relate in relationShip[key])
                        cmbVideoFormat.Items.Add(new KeyValue { Key = relate.Key, Value = dicVideoExtesion[relate.Key].ToString() });
                    if (cmbVideoFormat.Items.Count > 0)
                        cmbVideoFormat.SelectedIndex = 0;
                }
            }
        }

        private void InitializeForm()
        {
            relationShip.Add("标清（576p）", new List<KeyValuePair<string, short>> { new KeyValuePair<string, short>("MPG（IBP422）", 21), new KeyValuePair<string, short>("AVI（全I帧）", 25), new KeyValuePair<string, short>("MXF_MPEG2（MXF MPEG2）", 21) });
            relationShip.Add("小高清（720p）", new List<KeyValuePair<string, short>> { new KeyValuePair<string, short>("MPG（IBP422）", 21), new KeyValuePair<string, short>("AVI（全I帧）", 25), new KeyValuePair<string, short>("MXF_MPEG2（MXF MPEG2）", 21) });
            relationShip.Add("高清（1080i）", new List<KeyValuePair<string, short>> { new KeyValuePair<string, short>("MPG（IBP422）", 22), new KeyValuePair<string, short>("AVI（全I帧）", 26), new KeyValuePair<string, short>("MXF_MPEG2（MXF MPEG2）", 22), new KeyValuePair<string, short>("MXF_P2（P2）", 15), new KeyValuePair<string, short>("MXF_XDCAM_HD（XDCAM）", 22) });
            relationShip.Add("高清（1080psf）", new List<KeyValuePair<string, short>> { new KeyValuePair<string, short>("MPG（IBP422）", 22), new KeyValuePair<string, short>("AVI（全I帧）", 26), new KeyValuePair<string, short>("MXF_MPEG2（MXF MPEG2）", 22), new KeyValuePair<string, short>("MXF_P2（P2）", 15), new KeyValuePair<string, short>("MXF_XDCAM_HD（XDCAM）", 22) });
            relationShip.Add("高清（1080p）", new List<KeyValuePair<string, short>> { new KeyValuePair<string, short>("MPG（IBP422）", 22), new KeyValuePair<string, short>("AVI（全I帧）", 26), new KeyValuePair<string, short>("MXF_MPEG2（MXF MPEG2）", 22), new KeyValuePair<string, short>("MXF_P2（P2）", 15), new KeyValuePair<string, short>("MXF_XDCAM_HD（XDCAM）", 22) });
            foreach (string key in dicCaptureType.Keys)
                cmbCaptureType.Items.Add(new KeyValue { Key = key, Value = dicCaptureType[key].ToString() });
            if (cmbCaptureType.Items.Count > 0)
                cmbCaptureType.SelectedIndex = 0;

        }

        public ImporConfig CaptureConfig = null;

        private void rabButton_Click(object sender, EventArgs e)
        {
            RadioButton oRab = sender as RadioButton;
            if (oRab != null)
            {
                switch(oRab.Name)
                {
                    case "rabDouble":
                        rabDouble.Checked = true;
                    rabSingle.Checked = false;
                    break;
                    case "rabSingle":
                        rabDouble.Checked = false;
                    rabSingle.Checked = true;
                    break;

                }
               
            }
        }
    }

    public class ImporConfig
    {
        public ulong DiscCapacity { get; set; }

        public string DiscName { get; set; }

        public int VideoRate { get; set; }

        public string VideoExtension { get; set; }

        public int BurnType { get; set; }

        public bool RealTimeBurn { get; set; }

    }

}
